package homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Doudizhu {

    ArrayList<Puke> pukeArrayList = new ArrayList<Puke>();



    /**
     * 初始化
     */
    public void init(){
        pukeArrayList.add(new Puke("","大王",170));
        pukeArrayList.add(new Puke("","小王",160));

        String point = null;
        String type = null;


        for (int i = 0; i < 4; i++){
            int size = 0;
            if (i == 0){
                type = "♣";
                size += 2;
            }else if (i == 1){
                type = "♠";
                size += 4;
            }else if (i == 2){
                type = "♦";
                size += 1;
            }else{
                type = "♥";
                size += 3;
            }
            for (int j = 2; j < 15; j++){
                size = size + (j * 10);
                point = j + "";
                if (j == 11){
                    point = "J";
                }else if (j == 12){
                    point = "Q";
                }else if (j == 13){
                    point = "K";
                }else if (j == 14){
                    point = "A";
                }else if (j == 2){
                    size = size + 130;
                }
                pukeArrayList.add(new Puke(point,type,size));
                if (j == 2){
                    size = size - 130;
                }
                size = size - (j * 10);
            }
        }




    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(pukeArrayList);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<Puke> player1 = new ArrayList<>(pukeArrayList.subList(0,17));
        ArrayList<Puke> player2 = new ArrayList<>(pukeArrayList.subList(17,34));
        ArrayList<Puke> player3 = new ArrayList<>(pukeArrayList.subList(34,51));

        sort(player1);
        sort(player2);
        sort(player3);

        System.out.println("拥有" + pukeArrayList.get((int) (Math.random() * 52)) + "的人开始叫地主");
        System.out.println("地主牌为" + pukeArrayList.get(51) + "," + pukeArrayList.get(52) + "," + pukeArrayList.get(53));

        System.out.println("\n玩家1的牌为：" + player1);
        System.out.println("\n玩家2的牌为：" + player2);
        System.out.println("\n玩家3的牌为：" + player3);
        ArrayList<Puke> diZhu = new ArrayList<>();
        diZhu.add(pukeArrayList.get(51));
        diZhu.add(pukeArrayList.get(52));
        diZhu.add(pukeArrayList.get(53));
        pukeArrayList = new ArrayList<>();
        for (Puke obj : player1){
            pukeArrayList.add(obj);
        }
        for (Puke obj : player2){
            pukeArrayList.add(obj);
        }
        for (Puke obj : player3){
            pukeArrayList.add(obj);
        }
        for (Puke obj : diZhu){
            pukeArrayList.add(obj);
        }
    }


    public void sort(ArrayList arrayList){
        Collections.sort(arrayList, new Comparator<Puke>() {
            @Override
            public int compare(Puke o1, Puke o2) {
                return (o2.getSize() - o1.getSize());
            }
        });
    }




}
